package com.my.mymafengwo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.smallstrong.mafengwopro.R;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.my.mymafengwo.activity.TravelNotesActivity;
import com.my.mymafengwo.adapter.BaseAdapterHelper;
import com.my.mymafengwo.adapter.QuickAdapter;
import com.my.mymafengwo.bean.BoardcastData;
import com.my.mymafengwo.utils.UrlUtils;

/**
 * Created by smallstrong on 2015/12/7.
 * step
 */
public class BoradcastFragment extends android.support.v4.app.Fragment {
    private ListView lv;
    private QuickAdapter<BoardcastData.DataEntity.ListEntity> adapter;
    private HttpUtils http;
    private BoardcastData data;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = View.inflate(getActivity(), R.layout.fragment_broadcast, null);
        findViews(v);
        initData();
        registerListener();
        return v;
    }

    private void initData() {
        http = new HttpUtils();
        http.configDefaultHttpCacheExpiry(100 * 1024);
        http.send(HttpRequest.HttpMethod.GET, UrlUtils.MINE_BROADCAST_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                Gson gson = new Gson();
                data = gson.fromJson(result, BoardcastData.class);
                adapter = new QuickAdapter<BoardcastData.DataEntity.ListEntity>(getActivity(), R.layout.list_item_broadcast, data.getData().getList()) {
                    @Override
                    protected void convert(BaseAdapterHelper helper, BoardcastData.DataEntity.ListEntity item) {
                        helper.setText(R.id.tv_title, item.getMessage().getTitle());
                        helper.setText(R.id.tv_text, item.getMessage().getContent().getText());
                        helper.setText(R.id.tv_in, item.getMessage().getExt().getText());
                        helper.setImageUrl(R.id.iv_mes, item.getMessage().getContent().getImage().getUrl());
                    }
                };
                lv.setAdapter(adapter);
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    private void registerListener() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("ooo",position+"");
                Intent intent = new Intent(getActivity(), TravelNotesActivity.class);
                intent.putExtra("url",data.getData().getList().get(position).getUrl() );
                startActivity(intent);
            }
        });
    }

    private void findViews(View v) {
        lv = (ListView) v.findViewById(R.id.lv);
    }
}
